import pager from "@/assets/vue-mixin/pager";
import vPager from "@/components/list-page/v-pager";
import {
    fetchList,
    getRentContractPageList,
    handlePress,
    sendPnNum,
    confirmPnNum
} from "../../../api/finance-collection-receivable";
import editActual from "../edit/edit-actual";
import editData from "../edit/edit-data";
import editReceivable from "../edit/edit-receivable";
import editCheck from "../edit/edit-check";
import editReceive from "../edit/edit-receive";
import leadData from "../edit/lead-data";
import showActual from "../edit/show-actual";
import editAccounts from "../edit/edit-accounts";
import { downloadBlob } from "@/utils";
import { download } from "@/admin/api/common";
import { console } from "vuedraggable/src/util/helper";
import { mapState } from "vuex";
import contractView from "@/views/rent/contract/view.vue";
import BaseLoad from "@/assets/vue-mixin/BaseLoad";
export default {
    mixins: [pager, BaseLoad],
    components: {
        editActual,
        editData,
        editReceivable,
        vPager,
        editCheck,
        editReceive,
        leadData,
        showActual,
        editAccounts,
        contractView
    },
    data() {
        return {
            left: 0,
            height: 0,
            leftSearch: "",
            leftSearchForm: {},
            selectList: [],
            tabPosition: "left",
            tableData: [],
            selectionList: [],
            tableLoading: false,
            searchForm: { rentContractId: "xxx" },
            totalSearchData: [
                {
                    label: "付款项目",
                    value: "chargeProjectName",
                    type: "select",
                    model: "chargeProjectName",
                    children: [
                        {
                            label: "租金",
                            value: "租金",
                            type: "input",
                            data: ""
                        },
                        {
                            label: "租赁押金",
                            value: "租赁押金",
                            type: "input",
                            data: ""
                        },
                        {
                            label: "装修保证金",
                            value: "装修保证金",
                            type: "input",
                            data: ""
                        }
                    ]
                },
                {
                    label: "费用周期",
                    value: "costPeriodStart",
                    type: "date",
                    model: "costPeriodStart",
                    children: []
                },
                {
                    label: "应收日期",
                    value: "receivableDate",
                    type: "daterange",
                    model: "receivableDate",
                    children: []
                },
                {
                    label: "结算状态",
                    value: "payStatus",
                    type: "select",
                    model: "payStatus",
                    children: [
                        {
                            label: "未支付",
                            value: "01",
                            type: "input",
                            data: "01"
                        },
                        {
                            label: "已支付",
                            value: "02",
                            type: "input",
                            data: "02"
                        }
                    ]
                },
                {
                    label: "发送状态",
                    value: "sendStatus",
                    type: "select",
                    model: "sendStatus",
                    children: [
                        {
                            label: "未确认",
                            value: "01",
                            type: "input",
                            data: "01"
                        },
                        {
                            label: "待发送",
                            value: "03",
                            type: "input",
                            data: "03"
                        },
                        {
                            label: "已发送",
                            value: "02",
                            type: "input",
                            data: "02"
                        }
                    ]
                }
            ],
            searchDataFrom: {},
            tabTotal: 0,
            tabPageSize: 2,
            tabCurrentPage: 1,
            rentData: [],
            flag: false,
            btnText: "未接收",
            operText: "接收",
            clickCheck: false,
            activeIndex: -1,
            NewIcon: require("@/assets/images/new.png")
        };
    },
    computed: {
        ...mapState({
            charge_project_type: state => {
                let data =
                    state.dict.find(
                        item => item.type === "charge_project_type"
                    ) || {};
                let list = data.children || [];
                return data.children || [];
            },
            pay_status: state => {
                let data =
                    state.dict.find(item => item.type === "pay_status") || {};
                let list = data.children || [];
                return data.children || [];
            },
            send_status: state => {
                let data =
                    state.dict.find(item => item.type === "dend_status") || {};
                let list = data.children || [];
                return data.children || [];
            }
        }),
        tabPageParams: function() {
            return {
                current: this.tabCurrentPage,
                size: this.tabPageSize
            };
        }
    },
    mounted() {
        this.searchByKey();
        this.getTabListData();
    },

    methods: {
        //收款
        editActual() {
            if (this.selectionList.length == 1) {
                if (this.selectionList.find(item => item.payStatus == "02")) {
                    this.err("该收款中存在已支付数据");
                    return;
                }
                if (
                    this.selectionList.find(item => item.chargeProjectId == "1")
                ) {
                    this.err("租金不得手动收款");
                    return;
                }
                let ids = this.selectionList.map(item => item.receivableId);
                //不要带过去，让其自动生成
                this.$refs.editActual.show(
                    this.selectionList[0].rentContractId,
                    ids
                );
            } else {
                this.err("请选择一个要收款的项目");
            }
        },
        //新建应收
        editData(item) {
            this.$refs.editData.show(item);
        },
        //hxf-0818-合并收款
        handleMerge() {
            if (this.selectionList.length > 0) {
                if (this.selectionList.find(item => item.payStatus == "02")) {
                    this.err("该收款中存在已支付数据");
                    return;
                }
                let ids = this.selectionList.map(item => item.receivableId);
                //不要带过去，让其自动生成
                this.$refs.editActual.show(
                    this.selectionList[0].rentContractId,
                    ids
                );
            } else {
                this.err("请选择一个要收款的项目");
            }
        },
        //推送非税通知书
        sendPnNum() {
            if (this.selectionList.length === 0) {
                this.err("请选择一个要推送的项目");
            } else {
                if (
                    this.selectionList.find(item => item.chargeProjectId != "1")
                ) {
                    this.err("只能推送租金的数据");
                    return;
                }
                if (this.selectionList.find(item => item.sendStatus != "03")) {
                    this.err("只能推送待发送的数据");
                } else {
                    this.confirm("发送推送", "您确定要推送该数据吗？", () => {
                        let ids = this.selectionList.map(
                            item => item.receivableId
                        );
                        //this.tableLoading = true;
                        this.loadAction("请稍等...");
                        sendPnNum(ids.join(","))
                            .then(resp => {
                                if (resp.data.code === 0) {
                                    this.suc("操作成功");
                                    this.resetList();
                                    this.loadClose();
                                } else {
                                    this.err("操作失败");
                                    this.loadClose();
                                }
                                //this.loadClose()
                                //this.tableLoading = false;
                            })
                            .catch(e => {
                                this.loadClose();
                                this.err(e);
                            });
                    });
                }
            }
        },
        //确认推送非税单
        confirmPnNum() {
            if (this.selectionList.length === 0) {
                this.err("请选择一个要确认的项目");
            } else {
                if (
                    this.selectionList.find(item => item.chargeProjectId != "1")
                ) {
                    this.err("只能确认租金的数据");
                    return;
                }
                if (this.selectionList.find(item => item.sendStatus != "01")) {
                    this.err("只能确认未确认的数据");
                } else {
                    this.confirm("确认", "您确定该数据吗？", () => {
                        let ids = this.selectionList.map(
                            item => item.receivableId
                        );
                        confirmPnNum(ids.join(",")).then(resp => {
                            if (resp.data.code === 0) {
                                this.suc("操作成功");
                                this.resetList();
                            } else {
                                this.err("操作失败");
                            }
                        });
                    });
                }
            }
        },
        //应收调整
        editReceivable() {
            if (this.selectionList.length === 1) {
                if (this.selectionList[0].sendStatus != "01") {
                    this.err("只能调整待确认的数据");
                    return;
                }
                if (this.selectionList[0].payStatus == "01") {
                    this.restartSelectionList();
                    this.$refs.editReceivable.show(this.selectionList[0]);
                } else {
                    this.err("只能调整未支付的应收信息");
                }
            } else {
                this.err("请选择一个要调整应收的项目");
            }
        },
        //
        showData(contractId) {
            this.$refs.contractView.show(contractId);
            //this.$parent.$router.push({path: "/wy-rent/contract/view?id=" + contractId});
        },
        //hxf-0818-立即催款
        handlePress() {
            if (this.selectionList.length === 1) {
                if (this.selectionList[0].payStatus == "01") {
                    if (this.selectionList[0].sendStatus != "02") {
                        this.err("只能催款已推送的数据");
                        return;
                    }
                    let receivableId = this.selectionList[0].receivableId;
                    handlePress(receivableId).then(resp => {
                        if (resp.data.data) {
                            // this.$message({message: '本次催缴成功', type: 'success'});
                            //this.suc('删除成功')
                            //this.getListData()
                            this.$notify({
                                title: "本次催缴成功！",
                                // message: '本次催缴成功！',
                                type: "success",
                                offset: 170
                            });
                        } else {
                            this.err("催缴失败");
                        }
                    });
                } else {
                    this.err("只能催款未支付的应收信息");
                }
            } else {
                this.err("请选择一个要立即催款的项目");
            }
        },
        //hxf-0818-导入
        handleLeadingIn() {
            this.$refs.leadData.show();
        },
        //导出
        downloadData() {
            var url = "/api/financecollectionreceivable/downloadExcel";
            this.publicExport("应收", url, this.searchForm);
        },
        // hxf-0818-绑定输入框回车触发该事件以及点击确定后传值
        searchByKey(val) {
            if (this.searchDataFrom.chargeProjectName) {
                let sum = "";
                this.searchDataFrom.chargeProjectNames.map(item => {
                    if (sum.length > 0) {
                        sum += "," + item.value;
                    } else {
                        sum = item.value;
                    }
                });
                this.searchForm.chargeProjectName = sum;
            }

            // if (this.searchDataFrom.chargeProjectName) {
            //     let sum = ''
            //
            //     this.searchForm.chargeProjectName = this.searchDataFrom.chargeProjectName
            // }else{
            //     delete this.searchForm.chargeProjectName
            // }
            if (this.searchDataFrom.payStatus) {
                let sum = "";
                this.searchDataFrom.payStatuss.map(item => {
                    if (sum.length > 0) {
                        sum += "、" + item.value;
                    } else {
                        sum = item.value;
                    }
                });
                this.searchForm.payStatus = sum;
            } else {
                delete this.searchForm.payStatus;
            }
            if (this.searchDataFrom.sendStatus) {
                let sum = "";
                this.searchDataFrom.sendStatuss.map(item => {
                    if (sum.length > 0) {
                        sum += "," + item.value;
                    } else {
                        sum = item.value;
                    }
                });
                this.searchForm.sendStatus = sum;
            } else {
                delete this.searchForm.sendStatus;
            }

            if (this.searchDataFrom.costPeriodStart) {
                this.searchForm.costPeriodStart = this.searchDataFrom.costPeriodStart;
            } else {
                delete this.searchForm.costPeriodStart;
            }
            if (this.searchDataFrom.receivableDate) {
                this.searchForm.receivableDate = this.searchDataFrom.receivableDate;
            } else {
                delete this.searchForm.receivableDate;
            }

            if (val) {
                this.searchForm.projectName = val;
            } else {
                delete this.searchForm.projectName;
            }

            this.resetList();
        },
        downExcel() {
            var time = new Date();
            let name =
                time.getFullYear() +
                "-" +
                (time.getMonth() + 1) +
                "-" +
                time.getDate() +
                " " +
                time.getHours() +
                ":" +
                time.getMinutes() +
                ":" +
                time.getSeconds() +
                "-" +
                "应收列表.xlsx";

            console.log(this.searchForm.projectName);
            let url = "/api/financecollectionreceivable/downloadExcel";
            download(url, Object.assign(this.searchForm)).then(response => {
                downloadBlob(response.data, name);
            });
        },
        // hxf-0818-绑定重置按钮
        searchReset() {
            let rentContractId = this.searchForm.rentContractId;
            this.searchForm = {};
            this.searchForm.rentContractId = rentContractId;
            this.resetList();
        },
        getListData() {
            fetchList(Object.assign({}, this.pageParams, this.searchForm)).then(
                resp => {
                    this.setListData(resp.data);
                }
            );
        },
        restartSelectionList() {
            //不要带过去，让其自动生成
            this.selectionList[0].createBy = null;
            this.selectionList[0].createName = null;
            this.selectionList[0].createTime = null;
            this.selectionList[0].operateBy = null;
            this.selectionList[0].operateName = null;
            this.selectionList[0].operateTime = null;
        },
        // hxf-0819-分页的方法-将接口中拿到的数据，存到分页中
        setTabListData(resp) {
            let data = resp.data;
            this.tabTotal = data.total;
            this.rentData = data.records;
            this.handleTabClick();
        },
        // hxf-0819-分页的方法
        changeTabPageSize(val) {
            this.tabCurrentPage = 1;
            this.tabPageSize = val;
            this.getTabListData();
        },
        // hxf-0819-分页的方法
        changeTabCurrentPage(val) {
            this.tabCurrentPage = val;
            this.getTabListData();
        },
        // hxf-0819-分页的方法-接口中拿到的数据
        getTabListData() {
            //接口，根据实际接口改一下
            getRentContractPageList(
                Object.assign({}, this.tabPageParams, this.leftSearchForm)
            ).then(resp => {
                this.setTabListData(resp.data);
            });
        },
        handleInputSearch() {
            if (this.leftSearch) {
                this.leftSearchForm.projectName = this.leftSearch;
                // console.log('this.leftSearchForm---',this.$refs.notCheck, this.activeIndex)
                if (this.activeIndex !== -1) {
                    this.$refs.notCheck[this.activeIndex].innerHTML = "未接收";
                    this.$refs.notCheck[
                        this.activeIndex
                    ].style.backgroundColor = "#fff";
                }
                this.clickCheck = false;
            } else {
                delete this.leftSearchForm.projectName;
            }
            this.tabResetList();
        },
        tabResetList() {
            this.tabCurrentPage = 1;
            this.getTabListData();
        },
        editCheck(item) {
            this.$refs.editCheck.show(item);
        },
        editReceive(item) {
            //this.activeIndex = index
            this.$refs.editReceive.show(item);
        },
        handleTabClick(tab) {
            if (tab === undefined || tab === null || tab === "") {
                if (this.rentData.length > 0) {
                    this.searchForm.rentContractId = this.rentData[0].rentContractId;
                    this.resetList();
                }
            } else {
                this.searchForm.rentContractId = this.rentData[
                    tab.index
                ].rentContractId;
                this.resetList();
            }
        },
        handleClickCheck() {
            this.$refs.notCheck[this.activeIndex].innerHTML = "已接收";
            this.$refs.notCheck[this.activeIndex].style.backgroundColor =
                "#1563d1";
            this.clickCheck = true;
        },
        //查看
        showReceivableData(item) {
            this.$refs.showActual.show(item);
        },
        //生成核算单
        editAccounts(item) {
            this.$refs.editAccounts.show(item);
        }
    },
    filters: {
        mySubString(val) {
            if (val != null) {
                return val.substring(0, 7);
            }
            return "";
        },
        ellipsis(value) {
            if (!value) {
                return "";
            } else if (value.length > 8) {
                return value.slice(0, 8) + "...";
            } else {
                return value;
            }
        }
    }
};
